//
//  AdjustOrdersVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/24.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AdjustOrdersVC.h"
#import "DriverListModel.h"
#import "SelectDriverListCell.h"
@interface AdjustOrdersVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) SelectDriverListCell *selectDriverListCell;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) CJTextField *fieldSerach;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (weak, nonatomic) IBOutlet UIButton *btnSave;
//@property (nonatomic,strong) CJTextField *fieldSerach;
@end

@implementation AdjustOrdersVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"选择司机")];
    [self setBackLeftButton:@""];
    self.btnSave.backgroundColor = [UIColor cjMainColor];
    self.selectDriverListCell = [[SelectDriverListCell alloc]initWithFlex:nil reuseIdentifier:nil];
    
    [self initData];
    [self.view addSubview:self.tableView];
    [self createTopView];
    [self loadData];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
   
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
//    self.fieldSerach.text = @"";
}

- (IBAction)btnSaveAction:(UIButton *)sender {
    
    WS(ws);
    __block NSString *strCode= @"";
    __block NSString *carCode= @"";

    [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DriverListModel *mod = ws.modelArray[idx];
        if (mod.selected) {
            strCode = mod.partyCode;
            carCode = mod.vehicleCode;
            
        }
    }];
    
    if ([CJRegular getIsStrEmpty:strCode]) {
        [CJAlert showStringDismissWithDelay:@"请选择司机"];
    }else{
        if (self.blockSucess) {
            self.blockSucess(@{@"strCode":strCode,@"carCode":carCode});
            [self pop];
        }
    }
}

- (void)createTopView{

    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    FlexFrameView *header = [[FlexFrameView alloc] initWithFlex:@"SelectDiverSearchTopView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;

    [header layoutIfNeeded];
    //    [self.view addSubview:header];
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];
}

- (void)btnSerachRouteView{
    
//    if ([CJRegular getIsStrEmpty:self.fieldSerach.text]) {
////        [CJAlert showStringDismissWithDelay:@"请填写搜索内容"];
//    }else{
        [self.tableView.mj_header beginRefreshing];
//        [self downRefresh];
//    }
}

/// <#Description#>
- (void)loadData{

    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/driver/driverList",CJBaseUrl);
    NSDictionary *dicPar = @{@"pageNum":CJStringWithFormat(@"%d",self.page),
                             @"pageSize":@"10",
                             @"authStatus":@"3",
                             @"isFreeze":@"0",
                             @"workStatus":@"1",
                             @"driverType":@"3",
                             @"searchInfo":self.fieldSerach.text,
                             @"branchCodeExt":[CJControl getBranchCode]
    };

    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
                
        NSArray *list = responseObject[@"records"];
        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//        if (list.count > 0) {
            //            #import "CommonRouteModel.h"
            if ([ws.refreshType isEqualToString:@"down"]) {
                [ws.modelArray removeAllObjects];
            }
            
            [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               NSError *error;
                DriverListModel *model = [[DriverListModel alloc]initWithDictionary:obj error:&error];
                model.selected = NO;
                [ws.modelArray addObject:model];
            }];

            [ws.tableView.mj_header endRefreshing];
            [ws.tableView.mj_footer endRefreshing];
            
            if (ws.modelArray.count == total.intValue) {
                [ws.tableView.mj_footer endRefreshingWithNoMoreData];
            }
//        }else{
//            
//            [ws.tableView.mj_header endRefreshing];
//            [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        [ws.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        [ws.tableView.mj_header endRefreshing];
        [ws.tableView.mj_footer endRefreshing];
    }];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"SelectDriverListCell";
    SelectDriverListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SelectDriverListCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    DriverListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfiWithModel:model];
    WS(ws);
    cell.blockSelect = ^(id anyObject) {
         if (model.selected) {
             model.selected = NO;
         }else{
             model.selected = YES;
         }
        [ws.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            DriverListModel *mod = ws.modelArray[idx];
            if (idx == indexPath.row) {
                
            }else{
                
                if (mod.selected) {
//                    [ws.tableView reloadRowAtIndexPath:idx withRowAnimation:UITableViewRowAnimationFade];
                    mod.selected = NO;
                    [ws.tableView reloadRow:idx inSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];
                }
            }
        }];
        
         [ws.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
    };
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DriverListModel *model = self.modelArray[indexPath.row];
     if (model.selected) {
         model.selected = NO;
     }else{
         model.selected = YES;
     }
    WS(ws);
    [self.modelArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        DriverListModel *mod = ws.modelArray[idx];
        if (idx == indexPath.row) {
            
        }else{
            
            if (mod.selected) {
    //                    [ws.tableView reloadRowAtIndexPath:idx withRowAnimation:UITableViewRowAnimationFade];
                mod.selected = NO;
                [ws.tableView reloadRow:idx inSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];
            }
        }
    }];
    
     [self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight +  CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight  - self.btnSave.height - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(YES)
        ;
        WS(ws);
        _tableView.blockMjHeader = ^(BOOL isheader) {
            
            if (isheader) {
                [ws downRefresh];
            } else {
                [ws upRefresh];
            }
        };
    }
    return _tableView;
}

@end
